package org.gilgamesh.stool.search.es.param;

import cn.hutool.core.collection.CollectionUtil;
import org.gilgamesh.stool.search.constant.RangeType;
import org.gilgamesh.stool.search.es.ESSearchRequest;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 时间范围筛选
 *
 * @author Gilgamesh
 * @since 2021-08-10
 **/
public class RangeTimeParam implements Serializable {
    private static final long serialVersionUID = 9167070975653507490L;

    /**
     * 时间字段
     **/
    private String field;

    /**
     * 查询条件Map
     **/
    private Map<RangeType, Long> rangeMap;

    private ESSearchRequest request;

    /**
    * 获取时间字段
    **/
    public String field(){
        return this.field;
    }


    public RangeTimeParam(ESSearchRequest request){
        this.request = request;
    }

    /**
    * 方便链式创建查询条件时回到Request
    **/
    public ESSearchRequest toRequest(){
        return request;
    }

    /**
    * 设置时间字段并返回this
    **/
    public RangeTimeParam field(String field){
        this.field = field;
        return this;
    }

    /**
    * 获取查询条件
    **/
    public Map<RangeType, Long> rangeMap(){
        return this.rangeMap;
    }

    /**
    * 设置查询条件并返回this
    **/
    public RangeTimeParam rangeMap(Map<RangeType, Long> rangeMap){
        this.rangeMap = rangeMap;
        return this;
    }

    /* 小于 */
    public RangeTimeParam lt(long time){
        if(CollectionUtil.isEmpty(rangeMap)){
            rangeMap = new HashMap<>();
        }
        rangeMap.put(RangeType.LT,time);
        return this;
    }

    /* 大于 */
    public RangeTimeParam gt(long time){
        if(CollectionUtil.isEmpty(rangeMap)){
            rangeMap = new HashMap<>();
        }
        rangeMap.put(RangeType.GT,time);
        return this;
    }

    /* 小于等于 */
    public RangeTimeParam lte(long time){
        if(CollectionUtil.isEmpty(rangeMap)){
            rangeMap = new HashMap<>();
        }
        rangeMap.put(RangeType.LTE,time);
        return this;
    }

    /* 大于等于 */
    public RangeTimeParam gte(long time){
        if(CollectionUtil.isEmpty(rangeMap)){
            rangeMap = new HashMap<>();
        }
        rangeMap.put(RangeType.GTE,time);
        return this;
    }
}
